/************************************************************
 *  * Hyphenate CONFIDENTIAL
 * __________________
 * Copyright (C) 2016 Hyphenate Inc. All rights reserved.
 *
 * NOTICE: All information contained herein is, and remains
 * the property of Hyphenate Inc.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Hyphenate Inc.
 */

#import "EaseChatBarMoreView.h"

#define CHAT_BUTTON_SIZE CGSizeMake(80,90)
#define INSETS 10
#define MOREVIEW_COL 4
#define MOREVIEW_ROW 2
#define MOREVIEW_BUTTON_TAG 1000

#define More_BUTTON_SIZE CGSizeMake(80,60)
#define BTN_title_TOP 10

@implementation UIView (MoreView)

- (void)removeAllSubview
{
    for (UIView *view in self.subviews) {
        [view removeFromSuperview];
    }
}

@end

@interface EaseChatBarMoreView ()<UIScrollViewDelegate>
{
    EMChatToolbarType _type;
    NSInteger _maxIndex;
}

//@property (nonatomic, strong) UIScrollView *scrollview;
//@property (nonatomic, strong) UIPageControl *pageControl;
//
//@property (nonatomic, strong) UIView *topline;
//
//@property (nonatomic, strong) UIButton *photoButton;
//@property (nonatomic, strong) UIButton *takePicButton;
//@property (nonatomic, strong) UIButton *locationButton;
//@property (nonatomic, strong) UIButton *videoButton;
//@property (nonatomic, strong) UIButton *audioCallButton;
//@property (nonatomic, strong) UIButton *videoCallButton;

@end

@implementation EaseChatBarMoreView

+ (void)initialize
{
    // UIAppearance Proxy Defaults
    EaseChatBarMoreView *moreView = [self appearance];
    moreView.moreViewBackgroundColor = HOTCHATrgb(237, 237, 237);
}

- (instancetype)initWithFrame:(CGRect)frame type:(EMChatToolbarType)type
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        _type = type;
        [self setupSubviewsForType:_type];
    }
    return self;
}

- (void)setupSubviewsForType:(EMChatToolbarType)type
{
    CGRect frame = self.frame;
    frame.size.height = 196;
    self.frame = frame;
    
    self.backgroundColor = HOTCHATrgb(237, 237, 237);
    self.accessibilityIdentifier = @"more_view";
    
    UIView *backview = [[UIView alloc] init];
    backview.backgroundColor = [UIColor whiteColor];
    backview.layer.cornerRadius = 12;
    backview.clipsToBounds = YES;
    backview.layer.borderColor = HOTCHATrgb(217,217,217).CGColor;
    backview.layer.borderWidth = 0.5;
    [self addSubview:backview];
    [backview mas_updateConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self).with.insets(UIEdgeInsetsMake(0, 10, 10, 10));
    }];
    
    UIButton *HOTCHATbtn1 = [self getBtnWithName:NSLocalizedString(@"StrN048", nil) norImg:@"msg_pho_1" higImg:@"msg_pho_1_hig" tag:1];
    [self addSubview:HOTCHATbtn1];
    
//    UIButton *HOTCHATbtn2 = [self getBtnWithName:NSLocalizedString(@"StrN049", nil) norImg:@"msg_pho_2" higImg:@"msg_pho_2_hig" tag:2];
//    [self addSubview:HOTCHATbtn2];
    
    //    UIButton *HOTCHATbtn3 = [self getBtnWithName:NSLocalizedString([HOTCHATStringUtils getTheStringNO128], nil) norImg:@"msg_pho_3" higImg:@"msg_pho_3_hig" tag:3];
    //    [self addSubview:HOTCHATbtn3];
    
    //    UIButton *HOTCHATbtn4 = [self getBtnWithName:NSLocalizedString([HOTCHATStringUtils getThemy12], nil) norImg:@"msg_pho_4" higImg:@"msg_pho_4_hig" tag:3];
    //    [self addSubview:HOTCHATbtn4];
    
    //    UIButton *HOTCHATbtn5 = [self getBtnWithName:NSLocalizedString([HOTCHATStringUtils getTheStringNO153], nil) norImg:@"msg_pho_5" higImg:@"msg_pho_5_hig" tag:5];
    //    [self addSubview:HOTCHATbtn5];
    
//    UIButton *btn6 = [self getBtnWithName:NSLocalizedString(@"StrN050", nil) norImg:@"msg_pho_6" higImg:@"msg_pho_6_hig" tag:3];
//    [self addSubview:btn6];
    
}

- (UIView *)viewWithImg:(NSString *)img hig:(NSString *)hig name:(NSString *)name sel:(SEL)sel{
    UIView *view = [[UIView alloc] init];
    
    UIButton *btn = [[UIButton alloc] init];
    [btn setImage:[UIImage imageNamed:img] forState:(UIControlStateNormal)];
    [btn setImage:[UIImage imageNamed:hig] forState:(UIControlStateHighlighted)];
    [btn addTarget:self action:sel forControlEvents:(UIControlEventTouchUpInside)];
    [view addSubview:btn];
    
    UILabel *lab = [[UILabel alloc] init];
    lab.font = [UIFont systemFontOfSize:10];
    lab.textColor = HOTCHATrgb(102,102,102);
    lab.text = name;
    [view addSubview:lab];
    
    [btn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(view);
        make.centerX.equalTo(view);
    }];
    [lab mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(view);
        make.top.equalTo(btn.mas_bottom).offset(10);
    }];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:sel];
    view.userInteractionEnabled = YES;
    [view addGestureRecognizer:tap];
    
    return view;
}


- (UIButton *)btnWithImage:(UIImage *)aImage highlightedImage:(UIImage *)aHighLightedImage title:(NSString *)aTitle {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:aImage forState:UIControlStateNormal];
    [btn setImage:aHighLightedImage forState:UIControlStateHighlighted];
    [btn setTitle:aTitle forState:UIControlStateNormal];
    UIColor *color = HOTCHATrgb(102, 102, 102);
    [btn setTitleColor:color forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize: 10.0];
    btn.titleLabel.adjustsFontSizeToFitWidth = YES;
    btn.titleLabel.minimumScaleFactor = 0.7;
    //    btn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 30, 0);
    //    btn.titleEdgeInsets = UIEdgeInsetsMake(80, -60, 0, 0);
    [btn setImageEdgeInsets:UIEdgeInsetsMake(-btn.titleLabel.intrinsicContentSize.height, 0, 20, -btn.titleLabel.intrinsicContentSize.width)];
    [btn setTitleEdgeInsets:UIEdgeInsetsMake(btn.currentImage.size.height - 5, -btn.currentImage.size.width, 0, 0)];
    
    return btn;
}

- (void)insertItemWithImage:(UIImage *)image highlightedImage:(UIImage *)highLightedImage title:(NSString *)title
{
    //    CGFloat insets = (self.frame.size.width - MOREVIEW_COL * CHAT_BUTTON_SIZE.width) / 5;
    //    CGRect frame = self.frame;
    //    _maxIndex++;
    //    NSInteger pageSize = MOREVIEW_COL*MOREVIEW_ROW;
    //    NSInteger page = _maxIndex/pageSize;
    //    NSInteger row = (_maxIndex%pageSize)/MOREVIEW_COL;
    //    NSInteger col = _maxIndex%MOREVIEW_COL;
    //    UIButton *moreButton = [self btnWithImage:image highlightedImage:highLightedImage title:title];
    //    [moreButton setFrame:CGRectMake(page * CGRectGetWidth(self.frame) + insets * (col + 1) + CHAT_BUTTON_SIZE.width * col, INSETS + INSETS * 2 * row + CHAT_BUTTON_SIZE.height * row, CHAT_BUTTON_SIZE.width , CHAT_BUTTON_SIZE.height)];
    //    [moreButton setImage:image forState:UIControlStateNormal];
    //    [moreButton setImage:highLightedImage forState:UIControlStateHighlighted];
    //    [moreButton addTarget:self action:@selector(moreAction:) forControlEvents:UIControlEventTouchUpInside];
    //    moreButton.tag = MOREVIEW_BUTTON_TAG+_maxIndex;
    //    [_scrollview addSubview:moreButton];
    //    [_scrollview setContentSize:CGSizeMake(CGRectGetWidth(self.frame) * (page + 1), CGRectGetHeight(self.frame))];
    //    [_pageControl setNumberOfPages:page + 1];
    //    if (_maxIndex >=5) {
    //        frame.size.height = 150;
    //        _scrollview.frame = CGRectMake(0, 0, CGRectGetWidth(frame), CGRectGetHeight(frame));
    //        _pageControl.frame = CGRectMake(0, CGRectGetHeight(frame) - 20, CGRectGetWidth(frame), 20);
    //    }
    //    self.frame = frame;
    //    _pageControl.hidden = _pageControl.numberOfPages<=1;
}

- (void)updateItemWithImage:(UIImage *)image highlightedImage:(UIImage *)highLightedImage title:(NSString *)title atIndex:(NSInteger)index
{
    //    UIView *moreButton = [_scrollview viewWithTag:MOREVIEW_BUTTON_TAG+index];
    //    if (moreButton && [moreButton isKindOfClass:[UIButton class]]) {
    //        [(UIButton*)moreButton setImage:image forState:UIControlStateNormal];
    //        [(UIButton*)moreButton setImage:highLightedImage forState:UIControlStateHighlighted];
    //    }
}

- (void)removeItematIndex:(NSInteger)index
{
    //    UIView *moreButton = [_scrollview viewWithTag:MOREVIEW_BUTTON_TAG+index];
    //    if (moreButton && [moreButton isKindOfClass:[UIButton class]]) {
    //        [self _resetItemFromIndex:index];
    //        [moreButton removeFromSuperview];
    //    }
}

#pragma mark - private

- (void)_resetItemFromIndex:(NSInteger)index
{
    //    CGFloat insets = (self.frame.size.width - MOREVIEW_COL * CHAT_BUTTON_SIZE.width) / 5;
    //    CGRect frame = self.frame;
    //    for (NSInteger i = index + 1; i<_maxIndex + 1; i++) {
    //        UIView *moreButton = [_scrollview viewWithTag:MOREVIEW_BUTTON_TAG+i];
    //        if (moreButton && [moreButton isKindOfClass:[UIButton class]]) {
    //            NSInteger moveToIndex = i - 1;
    //            NSInteger pageSize = MOREVIEW_COL*MOREVIEW_ROW;
    //            NSInteger page = moveToIndex/pageSize;
    //            NSInteger row = (moveToIndex%pageSize)/MOREVIEW_COL;
    //            NSInteger col = moveToIndex%MOREVIEW_COL;
    //            [moreButton setFrame:CGRectMake(page * CGRectGetWidth(self.frame) + insets * (col + 1) + CHAT_BUTTON_SIZE.width * col, INSETS + INSETS * 2 * row + CHAT_BUTTON_SIZE.height * row, CHAT_BUTTON_SIZE.width , CHAT_BUTTON_SIZE.height)];
    //            moreButton.tag = MOREVIEW_BUTTON_TAG+moveToIndex;
    //            [_scrollview setContentSize:CGSizeMake(CGRectGetWidth(self.frame) * (page + 1), CGRectGetHeight(self.frame))];
    //            [_pageControl setNumberOfPages:page + 1];
    //        }
    //    }
    //    _maxIndex--;
    //    if (_maxIndex >=5) {
    //        frame.size.height = 150;
    //    } else {
    //        frame.size.height = 80;
    //    }
    //    self.frame = frame;
    //    _scrollview.frame = CGRectMake(0, 0, CGRectGetWidth(frame), CGRectGetHeight(frame));
    //    _pageControl.frame = CGRectMake(0, CGRectGetHeight(frame) - 20, CGRectGetWidth(frame), 20);
    //    _pageControl.hidden = _pageControl.numberOfPages<=1;
}

#pragma setter
//- (void)setMoreViewColumn:(NSInteger)moreViewColumn
//{
//    if (_moreViewColumn != moreViewColumn) {
//        _moreViewColumn = moreViewColumn;
//        [self setupSubviewsForType:_type];
//    }
//}
//
//- (void)setMoreViewNumber:(NSInteger)moreViewNumber
//{
//    if (_moreViewNumber != moreViewNumber) {
//        _moreViewNumber = moreViewNumber;
//        [self setupSubviewsForType:_type];
//    }
//}

- (void)setMoreViewBackgroundColor:(UIColor *)moreViewBackgroundColor
{
    _moreViewBackgroundColor = moreViewBackgroundColor;
    if (_moreViewBackgroundColor) {
        [self setBackgroundColor:HOTCHATrgb(237, 237, 237)];
    }
}

/*
 - (void)setMoreViewButtonImages:(NSArray *)moreViewButtonImages
 {
 _moreViewButtonImages = moreViewButtonImages;
 if ([_moreViewButtonImages count] > 0) {
 for (UIView *view in self.subviews) {
 if ([view isKindOfClass:[UIButton class]]) {
 UIButton *button = (UIButton *)view;
 if (button.tag < [_moreViewButtonImages count]) {
 NSString *imageName = [_moreViewButtonImages objectAtIndex:button.tag];
 [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
 }
 }
 }
 }
 }
 
 - (void)setMoreViewButtonHignlightImages:(NSArray *)moreViewButtonHignlightImages
 {
 _moreViewButtonHignlightImages = moreViewButtonHignlightImages;
 if ([_moreViewButtonHignlightImages count] > 0) {
 for (UIView *view in self.subviews) {
 if ([view isKindOfClass:[UIButton class]]) {
 UIButton *button = (UIButton *)view;
 if (button.tag < [_moreViewButtonHignlightImages count]) {
 NSString *imageName = [_moreViewButtonHignlightImages objectAtIndex:button.tag];
 [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateHighlighted];
 }
 }
 }
 }
 }*/

#pragma mark - UIScrollViewDelegate

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //    CGPoint offset =  scrollView.contentOffset;
    //    if (offset.x == 0) {
    //        _pageControl.currentPage = 0;
    //    } else {
    //        int page = offset.x / CGRectGetWidth(scrollView.frame);
    //        _pageControl.currentPage = page;
    //    }
}

#pragma mark - action

- (void)takePicAction{
    if(_delegate && [_delegate respondsToSelector:@selector(moreViewTakePicAction:)]){
        [_delegate moreViewTakePicAction:self];
    }
}

- (void)photoAction
{
    if (_delegate && [_delegate respondsToSelector:@selector(moreViewPhotoAction:)]) {
        [_delegate moreViewPhotoAction:self];
    }
}

- (void)locationAction
{
    if (_delegate && [_delegate respondsToSelector:@selector(moreViewLocationAction:)]) {
        [_delegate moreViewLocationAction:self];
    }
}

- (void)takeAudioCallAction
{
    if (_delegate && [_delegate respondsToSelector:@selector(moreViewAudioCallAction:)]) {
        [_delegate moreViewAudioCallAction:self];
    }
}

- (void)takeVideoCallAction
{
    if (_delegate && [_delegate respondsToSelector:@selector(moreViewVideoCallAction:)]) {
        [_delegate moreViewVideoCallAction:self];
    }
}

- (void)moreAction:(id)sender
{
    UIButton *button = (UIButton*)sender;
    if (button && _delegate && [_delegate respondsToSelector:@selector(moreView:didItemInMoreViewAtIndex:)]) {
        [_delegate moreView:self didItemInMoreViewAtIndex:button.tag-MOREVIEW_BUTTON_TAG];
    }
}
- (void)moreActionWithIndex:(NSInteger)index
{
    if (_delegate && [_delegate respondsToSelector:@selector(moreView:didItemInMoreViewAtIndex:)]) {
        [_delegate moreView:self didItemInMoreViewAtIndex:index];
    }
}

#pragma mark - Button

- (UIButton *)getBtnWithName:(NSString *)name norImg:(NSString *)norImg higImg:(NSString *)higImg tag:(NSInteger)tag{
    UIButton *btn = [[UIButton alloc] init];
    btn.tag = tag;
    [btn setTitle:name forState:(UIControlStateNormal)];
    UIImage *image = [UIImage imageNamed:norImg];
    [btn setImage:image forState:(UIControlStateNormal)];
    [btn setImage:[UIImage imageNamed:higImg] forState:(UIControlStateHighlighted)];
    [btn setTitleColor:HOTCHATrgb(51,51,51) forState:(UIControlStateNormal)];
    btn.titleLabel.font = [UIFont systemFontOfSize:10];
    
    NSInteger row = (tag - 1) / 3;
    NSInteger index = (tag - 1) % 3 + 1;
    
    CGFloat yy = 26 + (More_BUTTON_SIZE.height + 26) * row;
    CGFloat xspace = (HOTCHATUIWIDTH - 20 - More_BUTTON_SIZE.width * 3) / 6;
    CGFloat xx = 10 + xspace + More_BUTTON_SIZE.width * (index - 1) + xspace * (index - 1) * 2;
    
    btn.frame = CGRectMake(xx, yy, More_BUTTON_SIZE.width, More_BUTTON_SIZE.height);
    
    CGSize imgsize = image.size;
    
    //    btn.backgroundColor = [UIColor lightGrayColor];
    
    CGFloat leftspace = (More_BUTTON_SIZE.width - imgsize.width) / 2;
    CGFloat bottomspace = More_BUTTON_SIZE.height - imgsize.height;
    
    btn.imageEdgeInsets = UIEdgeInsetsMake(0, leftspace, bottomspace, leftspace);
    btn.titleEdgeInsets = UIEdgeInsetsMake(imgsize.height + BTN_title_TOP, -imgsize.width, 0, 0);
    [btn addTarget:self action:@selector(moreButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    return btn;
}

- (void)moreButtonAction:(UIButton *)btn{
    switch (btn.tag) {
        case 1:
            [self photoAction];
            break;
        case 2:
            [self takePicAction];
            break;
        case 3:
            [self moreActionWithIndex:btn.tag];
            break;
        case 4:
        case 5:
        case 6:
            
            //        case 4:
            //            [self takeAudioCallAction];
            //            break;
            //        case 5:
            //            [self takeVideoCallAction];
            //            break;
            //        case 6:
            //            [self moreActionWithIndex:btn.tag];
            //            break;
            
        default:
            break;
    }
}


@end
